package com.example.cunion.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cunion.entity.Coupon;
import com.example.cunion.entity.Coupon;
import com.example.cunion.entity.Message;
import com.example.cunion.exception.CunionException;
import com.example.cunion.mapper.ShopMapper;
import com.example.cunion.service.CouponService;
import com.example.cunion.mapper.CouponMapper;
import com.example.cunion.util.MessageTask;
import com.example.cunion.util.StringSnowflakeIdGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
* @author Aero
* @description 针对表【coupon(优惠券表)】的数据库操作Service实现
* @createDate 2023-10-22 23:18:44
*/
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon>
    implements CouponService{

    @Resource
    private CouponMapper couponMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private MessageTask messageTask;

    @Override
    public List<HashMap> searchUnCheckCouponByPage(HashMap map) {
        List<HashMap> list = couponMapper.searchUnCheckCouponByPage(map);
        return list;
    }

    @Override
    public Integer agreeCoupon(String id, String shopId) {
        Integer result = couponMapper.agreeCoupon(id);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        Message message = new Message();
        String userId = shopMapper.searchUserIdByShopId(shopId);
        StringSnowflakeIdGenerator generator = new StringSnowflakeIdGenerator(1, 1);
        String nextId = generator.nextId();
        String content = "平台发起您店铺的优惠券申请，请前往后台确认是否通过";
        message.setId(nextId);
        message.setReceiverId(userId);
        message.setIsCheck(1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("shopId", id);
        map.put("content", content);
        map.put("avatar", "http://eb118-file.cdn.bcebos.com/upload/1E63D742ADAD22DDD55B9AA050964F6E?x-bce-process=style/wm_ai");
        map.put("sendTime", DateUtil.current());
        map.put("result", "无需完善资料");
        // 使用ObjectMapper将HashMap转换为JSON字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = null;
        try {
            jsonString = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        // 将JSON字符串设置为消息内容
        message.setContent(jsonString);
        messageTask.sendAsync(userId, message);
        return result;
    }

    @Override
    public Integer rejectCoupon(String id) {
        Integer result = couponMapper.rejectCoupon(id);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        return result;
    }

    @Override
    public Integer addCoupon(HashMap map) {
        Integer result = couponMapper.addCoupon(map);
        if (result != 1){
            throw new CunionException("优惠券添加失败！");
        }
        return result;
    }

    @Override
    public List<HashMap> searchBusinessCheckByPage(HashMap map) {
        String shopId = shopMapper.searchShopIdByUserId(map.get("userId").toString());
        map.put("shopId", shopId);
        List<HashMap> list = couponMapper.searchBusinessCheckByPage(map);
        return list;
    }

    @Override
    public Integer updateCouponStatus(HashMap map) {
        Integer result = couponMapper.updateCouponStatus(map);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        return result;
    }

    @Override
    public Integer addCoupon(Coupon coupon) {
        int insert = couponMapper.insert(coupon);
        if (insert != 1){
            throw new CunionException("添加失败");
        }
        return insert;
    }

    @Override
    public Integer deletedCoupon(String couponId) {
        int result = couponMapper.deleteById(couponId);
        if (result != 1){
            throw new CunionException("删除失败！");
        }
        return result;
    }

    @Override
    public Integer updateCoupon(Coupon coupon) {
        int result = couponMapper.updateById(coupon);
        if (result != 1){
            throw new CunionException("修改失败");
        }
        return result;
    }

    @Override
    public List<Coupon> searchCoupon(Integer start, Integer length) {
        Page<Coupon> page = new Page<>(start, length);
        QueryWrapper<Coupon> QueryWrapper = new QueryWrapper<>();
        Page<Coupon> selectPage = couponMapper.selectPage(page, QueryWrapper);
        List<Coupon> list = selectPage.getRecords();
        return list;
    }
}




